<script lang="ts" setup>
import { ref } from 'vue'
import { GetInvatedData } from '@/api/modules/signintask'
import GetCacheData from '@/store/modules/NewPageCache'

// const imgDomain = 'https://img1.eprhan.cc'
const cacheData = GetCacheData()

let PageNum = cacheData.GetCacheData('signintaskInvated').currentPage
const loading = ref(true)
const total = ref(0)

const findFrom = ref({
  ...(cacheData.GetCacheData('signintaskInvated').fromData),
})
const dataList: any = reactive({
  list: [],
})

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.Openid && ({ Openid: findFrom.value.Openid })),
    ...(findFrom.value.ShareUserid && ({ ShareUserid: findFrom.value.ShareUserid })),
    ...(findFrom.value.ShareOpenid && ({ ShareOpenid: findFrom.value.ShareOpenid })),
  }
  await GetInvatedData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('signintaskInvated').currentSize)
  cacheData.SetCacheData('signintaskInvated', PageNum, cacheData.GetCacheData('signintaskInvated').currentSize, findFrom.value)
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('signintaskInvated', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('signintaskInvated').currentSize)
  cacheData.SetCacheData('signintaskInvated', val, cacheData.GetCacheData('signintaskInvated').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('signintaskInvated').currentPage || 1
})

onMounted(async () => {
  GetDataList(PageNum, cacheData.GetCacheData('signintaskInvated').currentSize)
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="被邀请人OpenId">
                <el-input v-model="findFrom.Openid" placeholder="请输入被邀请人OpenId" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="邀请人用户ID">
                <el-input v-model="findFrom.ShareUserid" placeholder="请输入邀请人用户ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="邀请人OpenId">
                <el-input v-model="findFrom.ShareOpenid" placeholder="请输入邀请人OpenId" clearable @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <!-- <el-button type="success" @click="AddData">
          添加
        </el-button> -->
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="邀请记录ID" width="100" />
        <el-table-column align="center" prop="shareSubscriber.userId" label="邀请人Id" width="100" />
        <el-table-column align="center" prop="shareSubscriber.scOpenId" label="邀请人OpenId" width="300" />
        <el-table-column align="center" prop="shareSubscriber" label="邀请人用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.shareSubscriber">
              未授权
            </div>
            <el-tooltip
              v-else
              class="box-item"
              effect="dark"
              :content="scope.row.shareSubscriber?.scOpenId"
              placement="top"
            >
              <div class="user-name">
                <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.shareSubscriber?.userAvatar" />
                <div>
                  <p style="margin: 0;">
                    {{ scope.row.shareSubscriber?.userNickName }}
                  </p>
                  <span>
                    {{ scope.row.shareSubscriber?.userPhone }}
                  </span>
                </div>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="subscriber.userId" label="被邀请人ID" width="100" />
        <el-table-column align="center" prop="subscriber.scOpenId" label="被邀请人OpenId" width="300" />
        <el-table-column align="center" prop="subscriber" label="被邀请人用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriber">
              未授权
            </div>
            <el-tooltip
              v-else
              class="box-item"
              effect="dark"
              :content="scope.row.subscriber?.scOpenId"
              placement="top"
            >
              <div class="user-name">
                <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriber?.userAvatar" />
                <div>
                  <p style="margin: 0;">
                    {{ scope.row.subscriber?.userNickName }}
                  </p>
                  <span>
                    {{ scope.row.subscriber?.userPhone }}
                  </span>
                </div>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="addTime" label="邀请时间" min-width="200" />
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('signintaskInvated').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 120px;
    margin-right: 10px;
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
